<?php
namespace app\common\model;

use think\Exception;
use think\Model;

class Base extends Model
{
    protected $autoWriteTimestamp = true;
    
    /**
     * 单条查询
     * @param array  $where   查询条件
     * @param string $field   查询字段
     * @param string $order   排序字段
     * @return array
     */
    public function getFind($where, $field = '*', $order = '')
    {
        try{
            $res = $this->where($where)->field($field)->order($order)->find();

            if (null === $res) {
                return ['code' => 'fail','msg' => $this->getError(), 'data' => []];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage(), 'data' => []];
        }
    }

    /**
     * 列表查询
     * @param array  $where    查询条件
     * @param string $field    查询字段
     * @param string $order    排序字段
     * @param int $is_page  0关闭分页  1开启分页
     * @param int $curr     页数
     * @param int $limit    条数
     * @return array
     */
    public function getList($where = [], $is_page = 0, $curr = 1, $limit = 10, $field='*', $order = '')
    {
        try{
            $res = $this->field($field)->where($where)->order($order);

            if ($is_page == 1) {
                $page = ($curr - 1) * $limit;
                $res = $res->limit($page, $limit);
            }

            $res = $res->select();

            if($is_page == 1){
                if ($res->isEmpty()) {
                    return ['code' => 'fail','msg' => '暂无数据', 'data' => []];
                } else {
                    return ['code' => 'ok','data' => $res];
                }
            }else{
                if (false == $res) {
                    return ['code' => 'fail','msg' => $this->getError(), 'data' => []];
                } else {
                    return ['code' => 'ok','data' => $res];
                }
            }
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage(), 'data' => []];
        }
    }

    /**
     * 添加数据
     * @param string $field 过滤字段
     * @param array  $data  添加数据
     * @return array
     */
    public function add($data, $field = [])
    {
        try{
            $res = $this->allowField($field)->save($data);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];

            } else {
                return ['code' => 'ok', 'data' => $this->id];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 批量添加数据
     * @param array $data 添加数据
     * @param string $field 过滤字段
     * @return array
     */
    public function addAll($data, $field = [])
    {
        try{
            $res = $this->allowField($field)->saveAll($data);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];

            } else {
                return ['code' => 'ok'];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 更新数据(带主键)
     * @param array $data   更新数据
     * @param array $field  指定字段
     * @return array
     */
    public function edit($data, $field = [])
    {
        try{
            $res = $this->allowField($field)->update($data);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok'];
            }
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage()];
        }
    }

    /**
     * 更新数据(不带主键)
     * @param array $data   更新数据
     * @param array $where  更新条件
     * @param array $field  指定字段
     * @return array
     */
    public function editWhere($data, $where= [], $field = [])
    {
        try{
            $res = $this->allowField([true])->where($where)->update($data);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok'];
            }
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage()];
        }
    }

    /**
     * 批量更新数据
     * @param array $data  带更新条件的数据
     * @return array
     */
    public function editAll($data)
    {
        try{
            $res = $this->isUpdate(true)->saveAll($data);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok'];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 删除数据
     * @param  string $where 删除的条件
     * @return array
     */
    public function del($where)
    {
        try{
            $res = $this->where($where)->delete();

            if ($res == 0) {
                return ['code' => 'fail','msg' => '数据错误'];
            } else {
                return ['code' => 'ok'];
            }
        }catch (Exception $e){
            return ['code' => 'error','msg' => $e->getMessage()];
        }
    }

    /**
     * 查询数量
     * @param array $where 查询条件
     * @return array
     */
    public function getNum($where = []){
        try {
            $res = $this->where($where)->count();
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 统计和
     * @param array $where 查询条件
     * @return array
     */
    public function getSum($where = [], $field){
        try {
            $res = $this->where($where)->sum($field);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 获取某个字段值
     * @param array  $where  查询条件
     * @param string $value  查询字段
     * @param string $order  排序字段
     * @return array
     */
    public function getVal($where = [], $value, $order = ''){
        try {
            $res = $this->where($where)->order($order)->value($value);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 获取某个列
     * @param array  $where  查询条件
     * @param string $value  查询字段
     * @return array
     */
    public function getCol($where = [], $value){
        try {
            $res = $this->where($where)->column($value);
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 字段自减
     * @param array  $where  查询条件
     * @param string $field  修改字段
     * @param int    $num    自减数量
     * @return array
     */
    public function setDecField($where = [], $field, $num = 1){
        try {
            $res = $this->where($where)->dec($field, $num)->update();
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }

    /**
     * 字段自增
     * @param array  $where  查询条件
     * @param string $field  修改字段
     * @param int    $num    自增数量
     * @return array
     */
    public function setIncField($where = [], $field, $num = 1){
        try {
            $res = $this->where($where)->inc($field, $num)->update();
            if (false === $res) {
                return ['code' => 'fail','msg' => $this->getError()];
            } else {
                return ['code' => 'ok','data' => $res];
            }
        }catch (Exception $e){
            return ['code' => 'fail','msg' => $e->getMessage()];
        }
    }
}